package com.ruoyi.edit.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.edit.service.OssService;
import com.ruoyi.edit.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.edit.domain.Vendor;
import com.ruoyi.edit.service.IVendorService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 供应商Controller
 * 
 * @author qiufeng
 * @date 2022-03-12
 */
@Controller
@RequestMapping("/system/vendor")
public class VendorController extends BaseController
{
    private String prefix = "system/vendor";

    @Autowired
    private IVendorService vendorService;

    @Autowired
    private OssService ossService;

    @Value("${oss.path}")
    public  String path;

    @Value("${oss.url}")
    public  String ossUrl;

    @GetMapping()
    public String vendor()
    {
        return prefix + "/vendor";
    }

    /**
     * 查询供应商列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Vendor vendor)
    {
        startPage();
        List<Vendor> list = vendorService.selectVendorList(vendor);
        return getDataTable(list);
    }

    /**
     * 导出供应商列表
     */
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Vendor vendor)
    {
        List<Vendor> list = vendorService.selectVendorList(vendor);
        ExcelUtil<Vendor> util = new ExcelUtil<Vendor>(Vendor.class);
        return util.exportExcel(list, "供应商数据");
    }

    /**
     * 新增供应商
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存供应商
     */
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Vendor vendor)
    {
        return toAjax(vendorService.insertVendor(vendor));
    }

    /**
     * 修改供应商
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Vendor vendor = vendorService.selectVendorById(id);
        mmap.put("vendor", vendor);
        return prefix + "/edit";
    }

    /**
     * 修改保存供应商
     */
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Vendor vendor)
    {
        return toAjax(vendorService.updateVendor(vendor));
    }

    /**
     * 删除供应商
     */
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(vendorService.deleteVendorByIds(ids));
    }


    /**
     * 添加图片
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, String id)
    {
        String originalName = file.getOriginalFilename();
        String extension = originalName.substring(originalName.lastIndexOf("."));
        String fileName = "admin" + "_"+ System.currentTimeMillis() + extension;
        String url = ossService.aliyunOssUpload(path+ "common/"+ fileName,file);
        Vendor vendor = vendorService.selectVendorById(id);
        if (AssertUtil.isEmpty(vendor)){
            return  AjaxResult.error("id不存在");
        }
        vendor.setCover(ossUrl+url);
        vendorService.updateVendor(vendor);
        return toAjax(1);
    }

    public static void main(String[] args) {
            String startStr = "2022-02-26 00:00:00";
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());//设置起时间
            cal.add(Calendar.MONTH, 13);//增加3个月
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = sdf.format(cal.getTime());
            Date validToDate = cal.getTime();
            System.out.println(sdf.format(validToDate));
        }
        /**
         * 获取两个日期之间的所有月份 (年月)
         *
         * @param startTime
         * @param endTime
         * @return：YYYY-MM
         */
        public static List<String> getMonthBetweenDate (String startTime, String endTime){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            // 声明保存日期集合
            List<String> list = new ArrayList<String>();
            try {
                // 转化成日期类型
                Date startDate = sdf.parse(startTime);
                Date endDate = sdf.parse(endTime);

                //用Calendar 进行日期比较判断
                Calendar calendar = Calendar.getInstance();
                while (startDate.getTime() <= endDate.getTime()) {
                    // 把日期添加到集合
                    list.add(sdf.format(startDate));
                    // 设置日期
                    calendar.setTime(startDate);
                    //把日期增加一天
                    calendar.add(Calendar.MONTH, 1);
                    // 获取增加后的日期
                    startDate = calendar.getTime();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return list;
        }
}
